class: center, middle, inverse, title-slide .title[ # Hands-on Exercise 7: Handling and Visualising Geospatial Data with R ] .author[ ### Dr. Kam Tin Seong
Assoc. Professor of Information Systems ] .institute[ ### School of Computing and Information Systems,
Singapore Management University ] .date[ ### 2020-2-15 (updated: 2022-05-16) ] --- ## Content .large[ - Mapping Geospatial Point Data with R - Choropleth Mapping with R ] --- ## Ex 1:Mapping Geospatial Point Data with R .pull-left[ In this hands-on exercise, you will learn how to create proportional symbol map by using **tmap** package. - By the end of this hands-on exercise, you will be able: - to import an aspatial data in R by using **readr** pakage, - to convert it into simple point feature by using **sf** package, and - to create interactive proportional symbol maps by using **tmap** package. ] --- ## Getting Started .pull-left[ Write a code chunk to check, install and launch **readr**, **sf** and **tmap** packages of R] -- .pull-right[ The solution: ] --- ## Importing wkt data .pull-left[ - *Well-known text (WKT)* is a human readable representation for spatial objects like points, lines, or enclosed areas on a map. ] -- .pull-right[ The solution: ```r schools <- read_sf("data/wkt/Schools.csv", options = "GEOM_POSSIBLE_NAMES=location") ``` DIY: ```r pubs <- read_sf("data/wkt/Pubs.csv", options = "GEOM_POSSIBLE_NAMES=location") apartments <- read_sf("data/wkt/Apartments.csv", options = "GEOM_POSSIBLE_NAMES=location") buildings <- read_sf("data/wkt/Buildings.csv", options = "GEOM_POSSIBLE_NAMES=location") employers <- read_sf("data/wkt/Employers.csv", options = "GEOM_POSSIBLE_NAMES=location") restaurants <- read_sf("data/wkt/Restaurants.csv", options = "GEOM_POSSIBLE_NAMES=location") ``` - After importing the data file into R, it is important for us to review the data object. ] --- ```r bldgs <- read_sf("data/wkt/Buildings.csv", options = "GEOM_POSSIBLE_NAMES=location") ``` --- ```r print(schools) ``` ``` ## Simple feature collection with 4 features and 4 fields ## Geometry type: POINT ## Dimension: XY ## Bounding box: xmin: -4701.463 ymin: 1607.984 xmax: -376.7505 ymax: 6556.032 ## CRS: NA ## # A tibble: 4 × 5 ## schoolId monthlyCost maxEnrollment location buildingId ## <chr> <chr> <chr> <POINT> <chr> ## 1 0 12.81244502 242 (-376.7505 1607.984) 662 ## 2 450 91.14351385 418 (-2597.448 3194.155) 943 ## 3 900 38.00537955 394 (-2539.158 6556.032) 262 ## 4 1350 73.19785215 384 (-4701.463 5141.763) 123 ``` --- ```r print(buildings) ``` ``` ## Simple feature collection with 1042 features and 4 fields ## Geometry type: POLYGON ## Dimension: XY ## Bounding box: xmin: -4762.191 ymin: -30.08359 xmax: 2650 ymax: 7850.037 ## CRS: NA ## # A tibble: 1,042 × 5 ## buildingId location buildingType maxOccupancy units ## <chr> <POLYGON> <chr> <chr> <chr> ## 1 1 ((350.0639 4595.666, 390.0633 459… Commercial "" "" ## 2 2 ((-1926.973 2725.611, -1948.191 2… Residental "12" "[48… ## 3 3 ((685.6846 1552.131, 645.9985 154… Commercial "" "[38… ## 4 4 ((-976.7845 4542.382, -1053.288 4… Commercial "" "" ## 5 5 ((1259.306 3572.727, 1299.255 357… Residental "2" "[23… ## 6 6 ((478.8969 1082.484, 473.6596 113… Commercial "" "" ## 7 7 ((-1920.823 615.7447, -1960.818 6… Residental "" "" ## 8 8 ((-3302.657 5394.354, -3301.512 5… Commercial "" "[13… ## 9 9 ((-600.5789 4429.228, -495.9506 4… Commercial "" "" ## 10 10 ((-68.75908 5379.924, -28.78232 5… Residental "5" "[10… ## # … with 1,032 more rows ``` --- ## Ploting a point symbol map .pull-left[ The code chunk below is used to create an interactive point symbol map. ```r tmap_mode("view") tm_shape(buildings)+ tm_polygons(col = "grey60", size = 1, border.col = "black", border.lwd = 1) ``` Things to learn from the code chunk: - `tmap_mode()` is used to switch the display from static mode (i.e. "plot") to interactive mode (i.e. "view"). - `tm_shape()` is used to create a **tmap-element** that specifies a spatial data object (i.e. point). - `tm_bubble()` is used to create a **tmap-element** that draws bubbles or small dots. Both colors and sizes of the bubbles can be mapped to data variables. ]] .pull-right[
] --- ## Ploting a point symbol map .pull-left[ The code chunk below is used to create an interactive point symbol map. ```r tmap_mode("view") tm_shape(buildings)+ tm_polygons(col = "grey60", size = 1, border.col = "black", border.lwd = 1) + tm_shape(employers) + tm_dots(col = "red") ``` ] .pull-right[
] --- ## Ploting a composite point symbol map .pull-left[ The code chunk below is used to create an interactive point symbol map. ```r tmap_mode("view") tm_shape(buildings)+ tm_polygons(col = "grey60", size = 1, border.col = "black", border.lwd = 1) + tm_shape(employers) + tm_dots(col = "red") + tm_shape(apartments) + tm_dots(col = "lightblue") + tm_shape(pubs) + tm_dots(col = "green") + tm_shape(restaurants) + tm_dots(col = "blue") + tm_shape(schools) + tm_dots(col = "yellow") ``` ] .pull-right[
]